<?php
namespace  Admin\Controller;
use Think\Controller;
use Think\Page;
class BrandController extends BaseController{
	public function add(){
		$model=D('Category');
		$data=$model->where('cid=0')->select();
		$this->assign('data',$data);
		$this->display('add');
	}
	public function addok(){
		if(isset($_POST['submit'])){
			$model=D('Category');
			$cid=I('post.cid');
			if($model->create()){
				if($model->where('id=$cid')->add()){
					$this->redirect('admin',array('cid'=>$cid),2,'添加成功...');
				}else{
					$this->error('添加失败','add');
				}
			}
		}else{
			$this->error($model->getError());
		}
		
	}
	//商品品牌管理
	public function admin(){
		$model=D('Category');
		$p=empty($_GET['p'])?1:$_GET['p'];
		//分页开始----------------------
		$count=$model->count();//总记录数
		$page=new Page($count,8);//输入总记录数   每页分配的数量
		//中间是留给更改样式的代码
		$page->lastSuffix=false;//不显示最后页吗
		$page->rollPage=5;//每页显示5个页码
		$page->setConfig('first', '[首页]');
		$page->setConfig('pre', '上一页');
		$page->setConfig('next', '下一页');
		$page->setConfig('last', '[末页]');
		$page->setConfig('theme', '共%TOTAL_ROW% 条记录,当前是%NOW_PAGE%/%TOTAL_PAGE%  %FIRST%%UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
		//中间是留给更改样式的代码
		$show=$page->show(); 
		$data=$model->relation(true)->order('id desc')->select();//分配数据-------注意---------注意-------注意--------
		$data=getTree($data);
		//$data=limit($page->firstRow.','.$page->listRows);  等同与下面的式子
		$data=array_slice($data, $page->firstRow, $page->listRows);
		//---------------------------
		$this->assign('p',$p);
		$this->assign('show',$show);
		$this->assign('data',$data);
		$this->display('admin');
	}
	//修改操作
	public function edit(){		
			$p=(int)$_GET['p'];
			$id=(int)$_GET['id'];
			$model=D('Category');
			$row=$model->find($id);
			$data=$model->where('cid=0')->select();
			$this->assign('p',$p);
			$this->assign('row',$row);
			$this->assign('data',$data);		
		$this->display('edit');
	}
	public function editok(){
		if(isset($_POST['submit'])){
			$id=(int)$_POST['id'];
			$p=$_POST['p'];
			//$p=empty($_POST['p'])?$_POST['p']:1;
			$model=D('Category');
			if($model->create()){
				if($model->save()!==flase){
					$this->redirect('admin',array('p'=>$p),2,'修改成功...');
				}else{
					$this->redirect('edit',array(),2,'修改失败...');
				}
			}else{
				$this->error($model->getError());
			}		
		}
	}
	public function deleteAll(){
		if(isset($_POST['deleteSubmit'])){
			$id=$_POST['id'];
			$p=$_POST['p'];
			$model=D('Category');
		    $ids=implode(',',$id);//将数组转为字符串
		    if($model->delete($ids)){
		    	$this->success('删除成功',U('admin',array('p'=>$p)));
		    }else{
		    	$this->error('删除失败',U('admin',array('p'=>$p)));
		    }
		}
		
	}
}